<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Email extends CI_Controller
{
	function __construct()
	{
		parent::__construct();

		//load helper
		$this->load->helper('login_helper');

			//load model

		$this->load->model('Memail');



    //load libraries

    $this->load->library('pagination');

    $this->load->library('form_validation');
	// session
	 session_start();
		 if ( !isset($_SESSION['row_number']))

			$_SESSION['row_number']= 12;
	load_lang("admin");
	check_login();
	}

	function test()
	{
		$this->load->Model("Mpermission");
		$dat = $this->Mpermission->info_from_id(5);
		print_r($dat);
		$per = $dat[0]["per_module"];
		echo $per;
		$a =unserialize($per);
		load_module($a);
	}





	//upddate
	function update()
	{
		$id= $this->uri->segment(4);
			if ($this->input->post('update'))

    {


      //set rules


        $this->form_validation->set_rules('em_email', 'Email', 'required|valid_email');




 //     $this->form_validation->set_rules('avatar', 'avatar', 'callback_upload_file');



      if ($this->form_validation->run() && $this->sub_update())
					echo "<script > alert('Cập nhật thành công!.'); document.location.href='".base_url()."admin/email/update/1' </script>";
//        redirect('admin/email/update/1', 'refresh');

      else
		{

        	$data['missing'] = $this->form_validation->_error_array;

		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/email/', 'refresh');

		}




		$data['main'] = 'email_update';
$data['result']= $this->Memail->info_form_id($id);
    $data['title'] = "Cập nhật email liên hệ";

		$this->load->view('admin/layout/template_main', $data);
	}
		// proccess data for update
	function sub_update()
	{
	$id= $this->uri->segment(4);
	if(isset($_POST["update"]))
		{

		$data1 = array(
			"em_email" => htmlentities($this->input->post("em_email"), ENT_QUOTES, 'UTF-8'),


			);
		//$data = array("name" => serialize($data1));


		if($this->Memail->update($data1,$id))
			return true;
		else
			return false;
		}
		else
			return false;
	}

	function create()
	{
	 if ($this->input->post('create'))

    {


      //set rules

      $this->form_validation->set_rules('name_vi', 'Tên  Đơn Vị(Tiếng Anh)', 'required');

      $this->form_validation->set_rules('name_en', 'Tên  Đơn Vị(Tiếng Việt)', 'required');




      if ($this->form_validation->run() && $this->sub_create())

        redirect('admin/unit/', 'refresh');

      else
		{

        	$data['missing'] = $this->form_validation->_error_array;

		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/unit/', 'refresh');

		}



		 //reload template

		$data['main'] = 'unit_create';

    $data['title'] = 'Thêm Nghành Hàng';

		$this->load->view('admin/template_main', $data);
	}
	function check_email()
	{
		$email=$this->uri->segment(4);
		if(!$this->Muser->check_email($email))
			echo "1";
		else
			echo "0";

	}

	function sub_create()
	{
		if(isset($_POST["create"]))
		{
/*		if($this->Muser->check_email(!$this->input->post("email")))
		{
			$this->form_validation->_error_array["email"] = "This Email have been existed .";

			return false;

		} */
		$name = array(
			"vi" => htmlentities($_POST["name_vi"],ENT_QUOTES,"UTF-8"),
			"en" => htmlentities($_POST["name_en"],ENT_QUOTES,"UTF-8"),
			);
		$data = array ( "name" => serialize($name) );



		if($this->Memail->insert($data))
			return true;
		else
			return false;
		}
		else
			return false;


	}


	function request()

	{

		//determine 'action'


		$require = array('show', 'delete');

		foreach ($_POST as $key=>$value)

		{

			if (in_array($key, $require))

			{

				$act = $key;

			}

		}



		//process 'action'

		if (! empty($act))

		{

			switch ($act)

			{

				case 'show': //show

				{

					$_SESSION['row_number'] = $this->input->post('row_number');

				} break;



				case 'delete': //delete

				{

					$this->Memail->delete();

				} break;

			}//end switch



			redirect('admin/unit/', 'refresh');

		}//end if

	}

}









?>
